package com.masu.knows.sys.controller;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 沈传尚
 * @date 2022/4/6
 */
// @RestController注解标记的控制器中的方法返回的字符串会认为是返回的内容
// @Controller 注解标记的控制器中的方法返回的字符串,会认为是要跳转的页面
@RestController
@RequestMapping("/v1/home")
public class HomeController {
    public static final GrantedAuthority STUDENT=new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER=new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping()
    public String index(@AuthenticationPrincipal UserDetails user){
        // 判断当前登录用户是否包含讲师身份
        if (user.getAuthorities().contains(TEACHER)){
            // 跳转到讲师首页 冲重定向到讲师首页
            return "/index_teacher.html";
        }else if (user.getAuthorities().contains(STUDENT)){
            return "/index_student.html";
        }
        // 既不是学生又不是讲师 去登录
        return "/login.html";
    }


}
